package com.ichifinga.airwave.shared;

import com.ichifinga.airwave.shared.enums.TextSizes;
import com.ichifinga.airwave.shared.enums.FlashTypes;
import com.ichifinga.airwave.shared.enums.TextWidths;
import com.ichifinga.airwave.shared.enums.DisplaySpeeds;
import com.ichifinga.airwave.shared.enums.ScrollTypes;
import com.ichifinga.airwave.shared.enums.TextShades;
import com.ichifinga.airwave.shared.enums.ShadeTypes;
import com.ichifinga.airwave.shared.enums.Colours;
import com.ichifinga.airwave.shared.entities.Phrase;
import com.ichifinga.airwave.shared.views.DisplaySurfaceView;
import com.ichifinga.airwave.shared.views.ScrollRadioGroup;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class EditPhraseActivity extends Activity
{
	private int _phraseId;
	private DisplaySurfaceView _dsv;
	private Phrase _phrase;
	
	ScrollRadioGroup _flashTypeRadioGroup;
	ScrollRadioGroup _textSizeRadioGroup;
	ScrollRadioGroup _textWidthRadioGroup;
	ScrollRadioGroup _displaySpeedRadioGroup;
	ScrollRadioGroup _shadeTypeRadioGroup;
	ScrollRadioGroup _scrollTypeRadioGroup;
	ScrollRadioGroup _textShadeRadioGroup;
	ScrollRadioGroup _backgroundColourRadioGroup;
	ScrollRadioGroup _colourOneRadioGroup;
	ScrollRadioGroup _colourTwoRadioGroup;
	ScrollRadioGroup _colourThreeRadioGroup;
		
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.editphraselayout);
		
		_phraseId = getIntent().getIntExtra("PhraseId", 1);
	
		Phrase.SetEditPhraseId(getBaseContext(), _phraseId);
		_phrase = Phrase.GetEditPhrase(getBaseContext());
		
		EditText et = (EditText)findViewById(R.id.phraseEditText);
		et.setText(_phrase._textToDisplay);
		
		_dsv = (DisplaySurfaceView)findViewById(R.id.MySurfaceView);
		_dsv._editMode = true;
		
		et.addTextChangedListener(new otcl());
		
		_flashTypeRadioGroup = (ScrollRadioGroup)findViewById(R.id.flashTypeRadioGroup);
		_textSizeRadioGroup = (ScrollRadioGroup)findViewById(R.id.textSizeRadioGroup);
		_textWidthRadioGroup = (ScrollRadioGroup)findViewById(R.id.textWidthRadioGroup);
		_displaySpeedRadioGroup = (ScrollRadioGroup)findViewById(R.id.displaySpeedRadioGroup);
		_shadeTypeRadioGroup = (ScrollRadioGroup)findViewById(R.id.shadeTypeRadioGroup);
		_scrollTypeRadioGroup = (ScrollRadioGroup)findViewById(R.id.scrollTypeRadioGroup);
		_textShadeRadioGroup = (ScrollRadioGroup)findViewById(R.id.textShadeRadioGroup);
		_backgroundColourRadioGroup = (ScrollRadioGroup)findViewById(R.id.backgroundColourRadioGroup);
		_colourOneRadioGroup = (ScrollRadioGroup)findViewById(R.id.colourOneRadioGroup);
		_colourTwoRadioGroup = (ScrollRadioGroup)findViewById(R.id.colourTwoRadioGroup);
		_colourThreeRadioGroup = (ScrollRadioGroup)findViewById(R.id.colourThreeRadioGroup);
		
		InitialiseRadioGroup(_flashTypeRadioGroup, _phrase._flashType.name(), FlashTypes.class);
		InitialiseRadioGroup(_textSizeRadioGroup, _phrase._textSize.name(), TextSizes.class);
		InitialiseRadioGroup(_textWidthRadioGroup, _phrase._textWidth.name(), TextWidths.class);
		InitialiseRadioGroup(_displaySpeedRadioGroup, _phrase._displaySpeed.name(), DisplaySpeeds.class);
		InitialiseRadioGroup(_shadeTypeRadioGroup, _phrase._shadeType.name(), ShadeTypes.class);
		InitialiseRadioGroup(_scrollTypeRadioGroup, _phrase._scrollType.name(), ScrollTypes.class);
		InitialiseRadioGroup(_textShadeRadioGroup, _phrase._textShade.name(), TextShades.class);
		InitialiseRadioGroup(_backgroundColourRadioGroup, _phrase._backgroundColour.name(),Colours.class);
		InitialiseRadioGroup(_colourOneRadioGroup, _phrase._colourOne.name(), Colours.class);
		InitialiseRadioGroup(_colourTwoRadioGroup, _phrase._colourTwo.name(), Colours.class);
		InitialiseRadioGroup(_colourThreeRadioGroup, _phrase._colourThree.name(), Colours.class);
		
		Button button = (Button)findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				_phrase.UpdatePhrase(getBaseContext());
				setResult(_phrase._phraseId);
				finish();				
			}
			
		});
		
		button = (Button)findViewById(R.id.button2);
		button.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{	
				finish();				
			}
			
		});
		
		InitialiseRadioGroups();
	}
	
	private void InitialiseRadioGroup(ScrollRadioGroup scrollRadioGroup, String enumString, Class<?> enumType)
	{	
		Enum<?>[] enumArray = (Enum[])enumType.getEnumConstants();
		
		RadioButton rb;
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
			(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		
		for(int counter = 0; counter < enumArray.length; counter ++)
		{
			rb = new RadioButton(this);
			if(enumType == Colours.class)
			{
				rb.setBackgroundColor(Colours.valueOf(enumArray[counter].name()).getAARRGGBB());
				rb.setPadding(0, 0, 40, 0);
			}
			else
			{
				rb.setText(enumArray[counter].name() + "    ");				
			}
			
			rb.setTag(enumArray[counter].name());
			rb.setLayoutParams(lp);
			
			scrollRadioGroup.addView(rb);			
		}
		
		rb = ((RadioButton)scrollRadioGroup.findViewWithTag(enumString));
		rb.setChecked(true);
		scrollRadioGroup.setOnCheckedChangeListener(new occl());
	}

	private class occl implements RadioGroup.OnCheckedChangeListener
	{
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId)
		{
			RadioButton rb = (RadioButton)group.findViewById(group.getCheckedRadioButtonId());
			
			if (group.getId() == R.id.flashTypeRadioGroup) {
				_phrase._flashType = FlashTypes.valueOf(rb.getTag().toString());
			} else if (group.getId() == R.id.textSizeRadioGroup) {
				_phrase._textSize = TextSizes.valueOf(rb.getTag().toString());
			} else if (group.getId() == R.id.scrollTypeRadioGroup) {
				_phrase._scrollType = ScrollTypes.valueOf(rb.getTag().toString());
			} else if (group.getId() == R.id.shadeTypeRadioGroup) {
				_phrase._shadeType = ShadeTypes.valueOf(rb.getTag().toString());
			} else if (group.getId() == R.id.textWidthRadioGroup) {
				_phrase._textWidth = TextWidths.valueOf(rb.getTag().toString());
			} else if (group.getId() == R.id.textShadeRadioGroup) {
				_phrase._textShade = TextShades.valueOf(rb.getTag().toString());
			} else if (group.getId() == R.id.displaySpeedRadioGroup) {
				_phrase._displaySpeed = DisplaySpeeds.valueOf(rb.getTag().toString());
			} else if (group.getId() == R.id.backgroundColourRadioGroup) {
				_phrase._backgroundColour = Colours.valueOf(rb.getTag().toString());
			} else if (group.getId() == R.id.colourOneRadioGroup) {
				_phrase._colourOne = Colours.valueOf(rb.getTag().toString());
			} else if (group.getId() == R.id.colourTwoRadioGroup) {
				_phrase._colourTwo = Colours.valueOf(rb.getTag().toString());
			} else if (group.getId() == R.id.colourThreeRadioGroup) {
				_phrase._colourThree = Colours.valueOf(rb.getTag().toString());
			}

			InitialiseRadioGroups();
			
			_dsv.RefreshPhrase(_phrase);
		}
	}
		
	private void InitialiseRadioGroups()
	{
		((View)_flashTypeRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		((View)_scrollTypeRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		((View)_textWidthRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		((View)_textSizeRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		((View)_displaySpeedRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		((View)_textShadeRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		((View)_shadeTypeRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		((View)_backgroundColourRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		((View)_colourOneRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		((View)_colourTwoRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		((View)_colourThreeRadioGroup.getParent().getParent()).setVisibility(View.VISIBLE);
		
		if(_phrase._flashType == FlashTypes.Police)
		{
			((View)_textShadeRadioGroup.getParent().getParent()).setVisibility(View.GONE);
			((View)_shadeTypeRadioGroup.getParent().getParent()).setVisibility(View.GONE);
			((View)_backgroundColourRadioGroup.getParent().getParent()).setVisibility(View.GONE);
			((View)_colourOneRadioGroup.getParent().getParent()).setVisibility(View.GONE);
			((View)_colourTwoRadioGroup.getParent().getParent()).setVisibility(View.GONE);
			((View)_colourThreeRadioGroup.getParent().getParent()).setVisibility(View.GONE);
		}
		
		if(_phrase._scrollType == ScrollTypes.Fit || _phrase._scrollType == ScrollTypes.Sequence || _phrase._scrollType == ScrollTypes.SequenceTimed)
		{
			((View)_textSizeRadioGroup.getParent().getParent()).setVisibility(View.GONE);
		}

		if(_phrase._scrollType == ScrollTypes.Fit || _phrase._scrollType == ScrollTypes.Sequence)
		{
			((View)_displaySpeedRadioGroup.getParent().getParent()).setVisibility(View.GONE);
		}
		
		if(_phrase._textShade == TextShades.Chill ||_phrase._textShade == TextShades.Fire || _phrase._textShade == TextShades.Ice ||
				_phrase._textShade == TextShades.Olympic ||	_phrase._textShade == TextShades.Rainbow)
		{	
			((View)_colourOneRadioGroup.getParent().getParent()).setVisibility(View.GONE);
			((View)_colourTwoRadioGroup.getParent().getParent()).setVisibility(View.GONE);
			((View)_colourThreeRadioGroup.getParent().getParent()).setVisibility(View.GONE);
		}
		else if(_phrase._textShade == TextShades.TwoColour)
		{	
			((View)_colourThreeRadioGroup.getParent().getParent()).setVisibility(View.GONE);
			
		}
		else if(_phrase._textShade == TextShades.OneColour)
		{
			((View)_colourTwoRadioGroup.getParent().getParent()).setVisibility(View.GONE);
			((View)_colourThreeRadioGroup.getParent().getParent()).setVisibility(View.GONE);
		}
	}
	
	private class otcl implements TextWatcher
	{
		@Override
		public void afterTextChanged(Editable arg0) {
			
			String test = arg0.toString();
			_phrase._textToDisplay = 
				test.length() > 0
				? test
				: "[ENTER TEXT]";
				
			_dsv.RefreshPhrase(_phrase);
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
		{	
		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
		{
		}
	}
}
